<template>
  <div class="app-container">
    <avue-crud ref="crud"
               :page="page"
               :data="tableData"
               :table-loading="tableLoading"
               :option="tableOption"
               :permission="permissionList"
               @on-load="getPageList"
               @refresh-change="refreshChange"
               @row-update="handleUpdate"
               @row-save="handleSave"
               @row-del="handleDel"
               @sort-change="sortChange"
               @search-change="searchChange"
               @selection-change="selectionChange">
      <template slot="sex" slot-scope="scope">
        <el-tag size="mini" effect="light"
                :type="scope.row.sex == '1' ? '' : scope.row.sex == '2' ? 'danger' : 'warning'">
          {{ scope.row.$sex }}
        </el-tag>
      </template>
    </avue-crud>
  </div>
</template>

<script setup name="wxuser">
import {checkPermi, checkRole} from "@/utils/permission"
// import {getPage, getObj, addObj, putObj, delObj, synchroWxUser, updateRemark, tagging} from '@/api/wxma/wxuser'
import {getPage, addObj, putObj, delObj} from '@/api/wxma/wxuser'
import {tableOption} from '@/const/crud/wxma/wxuser'
import {ref, computed, reactive} from "vue";
import {ElMessage} from "element-plus";
const { proxy } = getCurrentInstance();

const tableData = ref([]);
const page = reactive({
  total: 0, // 总页数
  currentPage: 1, // 当前页数
  pageSize: 20, // 每页显示多少条
  ascs: [],
  descs: 'create_time'
})
const paramsSearch = ref({})
const tableLoading = ref(false);
const selectionData = ref([]);

function selectionChange(list) {
  selectionData.value = list
}

function searchChange(params, done) {
  // params = this.filterForm(params)
  paramsSearch.value = params
  page.currentPage = 1
  getPageList(page, params)
  done()
}

function sortChange(val) {
  let prop = val.prop ? val.prop.replace(/([A-Z])/g, "_$1").toLowerCase() : '';
  if (val.order == 'ascending') {
    page.descs = []
    page.ascs = prop
  } else if (val.order == 'descending') {
    page.ascs = []
    page.descs = prop
  } else {
    page.ascs = []
    page.descs = []
  }
  getPageList(page)
}

function getPageList(page, params) {
  tableLoading.value = true
  getPage(Object.assign({
    current: page.currentPage,
    size: page.pageSize,
    descs: page.descs,
    ascs: page.ascs,
    appType: '1'
  }, params, paramsSearch.value)).then(response => {
    tableData.value = response.data.records
    page.total = response.data.total
    page.currentPage = page.currentPage
    page.pageSize = page.pageSize
    tableLoading.value = false
  }).catch(() => {
    tableLoading.value = false
  })
}

function update(row, index) {
  proxy.$prompt('请输入备注', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    inputPattern: /\S/,
    inputErrorMessage: '输入不能为空'
  }).then(({value}) => {
    tableLoading.value = true
    // row.remark = value
    // updateRemark(row).then(response => {
    //   tableLoading.value = false
    //   if (response.code == 200) {
    //     tableData.value.splice(index, 1, Object.assign({}, row))
    //     ElMessage({ message: '修改成功', type: 'success', duration: 5 * 1000 })
    //
    //     // this.$message({
    //     //   showClose: true,
    //     //   message: '修改成功',
    //     //   type: 'success'
    //     // })
    //     getPageList(page)
    //   } else {
    //     ElMessage({ message: response.msg, type: 'error' })
    //     // this.$message.error(response.msg)
    //   }
  // }).catch(() => {
  //   tableLoading.value = false
  // })
}

).catch(() => {
});
}

function handleDel(row, index) {
  proxy.$prompt('是否确认删除', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then((function () {
    return delObj(row.id)
  }).then(data => {
    ElMessage({message: '删除成功', type: 'success', duration: 5 * 1000})
    // _this.$message({
    //   showClose: true,
    //   message: '删除成功',
    //   type: 'success'
    // })
    getPageList(page)
  }).catch(function (err) {
  }))
}

/**
 * @title 数据更新
 * @param row 为当前的数据
 * @param index 为当前更新数据的行数
 * @param done 为表单关闭函数
 *
 **/
function handleUpdate(row, index, done, loading) {
  putObj(row).then(data => {
    ElMessage({message: '修改成功', type: 'success', duration: 5 * 1000})

    // this.$message({
    //   showClose: true,
    //   message: '修改成功',
    //   type: 'success'
    // })
    done()
    getPageList(page)
  }).catch(() => {
    loading()
  })
}

/**
 * @title 数据添加
 * @param row 为当前的数据
 * @param done 为表单关闭函数
 *
 **/
function handleSave(row, done, loading) {
  addObj(row).then(data => {
    ElMessage({message: '添加成功', type: 'success', duration: 5 * 1000})

    // this.$message({
    //   showClose: true,
    //   message: '添加成功',
    //   type: 'success'
    // })
    done()
    getPageList(page)
  }).catch(() => {
    loading()
  })
}

/**
 * 刷新回调
 */
function refreshChange(page) {
  getPageList(page)
}

const permissionList = computed(() => {
  return {
    addBtn: checkPermi(['wxma:wxuser:add']),
    delBtn: checkPermi(['wxma:wxuser:del']),
    editBtn: checkPermi(['wxma:wxuser:edit']),
    viewBtn: checkPermi(['wxma:wxuser:get'])
  }
})


</script>

<style lang="scss" scoped>
</style>
